package com.example.componentstool.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.*;
import android.util.Log;


import java.util.List;

public class AppListManagerUtils {

    /**
     * 查询软件列表：
     * @param packageManager
     */
    public static void printAppList(PackageManager packageManager) {
        // 获取所有已安装的应用程序
        List<ApplicationInfo> appList = packageManager.getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES);

        // 遍历应用程序列表并打印应用程序名称
        for (ApplicationInfo appInfo : appList) {
            String appName = packageManager.getApplicationLabel(appInfo).toString();
            Log.d("AppListManager", "应用程序名称：" + appName);
            Log.d("AppInfo",appInfo.toString());
        }
    }
    /**
     * 获取某个Android应用的服务、广播接收器、活动和内容提供器的启用禁用状态：
     */
    public static void printComponentInfo(Context context, String packageName, String componentName) {
        PackageManager packageManager = context.getPackageManager();
        ComponentName component = new ComponentName(packageName, componentName);

        try {
            ActivityInfo activityInfo = packageManager.getActivityInfo(component, PackageManager.GET_META_DATA);
            ServiceInfo serviceInfo = packageManager.getServiceInfo(component, PackageManager.GET_META_DATA);
            ProviderInfo providerInfo = packageManager.getProviderInfo(component, PackageManager.GET_META_DATA);

            // 获取广播接收器信息
            Intent intent = new Intent();
            intent.setComponent(component);
            ResolveInfo resolveInfo = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
            ActivityInfo receiverInfo = resolveInfo != null ? resolveInfo.activityInfo : null;


            // 打印组件信息
            if (activityInfo != null) {
                Log.d("ComponentInfoManager", "活动信息：" + activityInfo.toString());
            }
            if (serviceInfo != null) {
                Log.d("ComponentInfoManager", "服务信息：" + serviceInfo.toString());
            }
            if (providerInfo != null) {
                Log.d("ComponentInfoManager", "内容提供器信息：" + providerInfo.toString());
            }
            if (receiverInfo!=null){
                Log.d("ComponentInfoManager", "广播信息：" + receiverInfo.toString());
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 禁用组件
    public static void disableComponent(Context context, String packageName, String componentName) {
        setComponentEnabled(context, packageName, componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
    }

    // 启用组件
    public static void enableComponent(Context context, String packageName, String componentName) {
        setComponentEnabled(context, packageName, componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
    }

    // 设置组件的启用禁用状态
    private static void setComponentEnabled(Context context, String packageName, String componentName, int newState) {
        PackageManager packageManager = context.getPackageManager();
        ComponentName component = new ComponentName(packageName, componentName);

        packageManager.setComponentEnabledSetting(component, newState, PackageManager.DONT_KILL_APP);
    }
}
